{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 169,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import glob"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "metadata": {},
   "outputs": [],
   "source": [
    "path0 = r'D:\\GitHub\\rich diseases\\imageCrop\\rice_leaf_diseases\\Bacterial leaf blight\\0'\n",
    "path1 = r'D:\\GitHub\\rich diseases\\imageCrop\\rice_leaf_diseases\\Bacterial leaf blight\\1'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": [
       "['D:',\n",
       " 'GitHub',\n",
       " 'rich diseases',\n",
       " 'imageCrop',\n",
       " 'rice_leaf_diseases',\n",
       " 'Bacterial leaf blight',\n",
       " '1',\n",
       " 'img-0.jpg']"
      ]
     },
     "metadata": {},
     "execution_count": 171
    }
   ],
   "source": [
    "files = glob.glob(path1+'\\\\*.jpg')\n",
    "files[0].split('\\\\')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "metadata": {},
   "outputs": [],
   "source": [
    "# for i,file_name in enumerate(files):\n",
    "#     newname = path0 + '\\\\img-' + str(i) +'.jpg'\n",
    "#     os.rename(file_name,newname)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "metadata": {},
   "outputs": [],
   "source": [
    "def run(path):\n",
    "    files = glob.glob(path+'\\\\*.jpg')\n",
    "    for i,file_name in enumerate(files):\n",
    "        try:\n",
    "            newname = path + '\\\\img-' + str(i) +'.jpg'\n",
    "            os.rename(file_name,newname)\n",
    "        except FileExistsError as e:\n",
    "            newname = path + '\\\\img' + str(i) +'.jpg'\n",
    "            os.rename(file_name,newname)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "metadata": {},
   "outputs": [],
   "source": [
    "run(path0)\n",
    "run(path1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}